import * as util from './util'
import config from '@/utils/config'
/**
 * 获取当前运行的终端(App H5 小程序)
 * https://uniapp.dcloud.io/platform
 */
export const getPlatform = () => {
	// #ifdef APP-PLUS
	const platform = 'App'
	// #endif
	// #ifdef APP-PLUS-NVUE
	const platform = 'App'
	// #endif
	// #ifdef H5
	const platform = 'H5'
	// #endif
	// #ifdef MP-WEIXIN
	const platform = 'MP-WEIXIN'
	// #endif
	// #ifdef MP-ALIPAY
	const platform = 'MP-ALIPAY'
	// #endif
	// #ifdef MP-BAIDU
	const platform = 'MP-BAIDU'
	// #endif
	// #ifdef MP-TOUTIAO
	const platform = 'MP-TOUTIAO'
	// #endif
	// #ifdef MP-QQ
	const platform = 'MP-QQ'
	// #endif
	// #ifdef MP-360
	const platform = 'MP-360'
	// #endif
	return platform
}

/**
 * 显示成功提示框
 */
export const showSuccess = (msg, callback) => {
	uni.showToast({
		title: msg,
		icon: 'success',
		mask: true,
		duration: 1500,
		success() {
			callback && callback()
		}
	})
}

/**
 * 显示失败提示框
 */
export const showError = (msg, callback) => {
	uni.showModal({
		title: '友情提示',
		content: msg,
		showCancel: false,
		success(res) {
			callback && callback()
		}
	})
}

/**
 * 显示纯文字提示框
 */
export const showToast = msg => {
	uni.showToast({
		title: msg,
		icon: 'none'
	})
}

/**
 * tabBar页面路径列表 (用于链接跳转时判断)
 * tabBarLinks为常量, 无需修改
 */
export const getTabBarLinks = () => {
	const tabBarLinks = [
		'pages/manghe/manghe',
		'pages/shop/shop',
		'pages/brand/brand',
		'pages/mine/mine'
	]
	return tabBarLinks
}

/**
 * 跳转到指定页面url
 * 支持tabBar页面
 * @param {string}  url
 * @param {object}  query
 */
export const navTo = (url, query) => {
	// console.log('nav query=====', query)
	// console.log('nav url=====', url)
	if (!url || url.length == 0) {
		return false
	}
	// tabBar页面, 使用switchTab
	if (util.inArray(url, getTabBarLinks())) {
		uni.switchTab({
			url: `/${url}`
		})
		return true
	}
	// 生成query参数
	const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''
	// 普通页面, 使用navigateTo
	uni.navigateTo({
		url: `/${url}${queryStr}`
	})
	return true
}


/**
 * 关闭当前页面并跳转 uni.redirectTo
 */
export const redirectTo = (url, query = {}) => {
	if (!url || url.length == 0) {
		return false
	}
	// 生成query参数
	const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''

	uni.redirectTo({
		url: `/${url}${queryStr}`
	});
}

/**
 * 关闭所有页面并跳转 uni.reLaunch
 */
export const reLaunchTo = (url, query = {}) => {
	if (!url || url.length == 0) {
		return false
	}
	// 生成query参数
	const queryStr = !util.isEmpty(query) ? '?' + util.urlEncode(query) : ''

	uni.reLaunch({
		url: `/${url}${queryStr}`
	});
	return true
}



/**
 * 本地存储
 */

export class store {
	static get = (key) => {
		if (!key) return
		return uni.getStorageSync(key);
	}
	static set = (key, value) => {
		if (!key) return
		return uni.setStorageSync(key, value);
	}
	static remove = (key) => {
		if (!key) return
		return uni.removeStorageSync(key);
	}
	static clear = () => {
		return uni.clearStorageSync();
	}
}

/**
 * 时间戳转时间
 */
export const timestampToTime = (timestamp, type) => {
	var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear();
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
	var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
	var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
	var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
	var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
	if (type == 'YMD') {
		return `${Y}-${M}-${D}`;
	}
	if (type == 'HMS') {
		return `${h}:${m}:${s}`;
	}
	if (type == 'M') {
		return M;
	}
	if (type == 'D') {
		return D;
	}
	if (type == 'h') {
		return h;
	}
	return `${Y}-${M}-${D} ${h}:${m}:${s}`;
}


/**
 * 判断是否是时间格式
 */
export const isTime = (data) => {
	return isNaN(data) && !isNaN(Date.parse(data))
}

/**
 * 判断是否为空
 */
export const isEmpty = (params) => {
	if (params == undefined || params == null || params == false || params == 0) {
		return true;
	}
	if (params.constructor == String) {
		if (params == '' || params.length == 0 || params.trim() == '') {
			return true;
		} else {
			return false;
		}
	}
	if (params.constructor == Array) {
		if (params.length == 0 || params == []) {
			return true;
		} else {
			return false;
		}
	}
	if (params.constructor == Object) {
		if (Object.keys(params).length == 0 || JSON.stringify(params) == "{}") {
			return true;
		} else {
			return false;
		}
	}
	return false;
}


// 文字省略号
export const textEllipsis = (text, value) => {
	console.log('text====', text)
	if (typeof(text) == 'string') {
		if (text.length > value) {
			return text.substring(0, value) + '...'
		} else {
			return text
		}
	}
}

/**
 * 账号截取
 */
export const accountFormat = (value, num) => {
	return value.replace(/(\w{6})\w*(\w{4})/, '$1********$2');
}


/**
 * 发起微信支付请求
 * @param {Object} 参数
 */
export const requestPayment = (option, payType = 1) => {
	// payType 1 微信  2支付宝
	console.log('支付参数====')
	console.log(option.res)
	console.log(payType)
	return new Promise((resolve, reject) => {
		uni.requestPayment({
			provider: 'wxpay',
			// #ifdef APP
			orderInfo: payType == 1 ? option.res : option.result.res,
			// #endif

			// #ifdef MP-WEIXIN
			timeStamp: option.res.timeStamp.toString(),
			nonceStr: option.res.nonceStr,
			package: option.res.package,
			signType: option.res.signType,
			paySign: option.res.sign,
			// #endif

			success: res => resolve(res),
			fail: res => reject(res)
		})
	})
}


/**
 * 获取状态栏高度
 * @param {Object} 参数
 */
export const statusBarHeight = () => {
	return uni.getSystemInfoSync().statusBarHeight
}

export const screenHeight = () => {
	return uni.getSystemInfoSync().screenHeight
}

export const screenWidth = () => {
	return uni.getSystemInfoSync().screenWidth
}


/**
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性：max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * @param html
 * @returns {void|string|*}
 */
export const formatRichText = (html) => {
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
			'max-width:100%;');
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi,
		'<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
	return newContent;
}


/**
 * 盲盒类型名称 
 */

export const getSignText = (type) => {
	if (type == 1) {
		return '隐藏款'
	} else if (type == 2) {
		return '超神款'
	} else if (type == 3) {
		return '欧气款'
	} else if (type == 4) {
		return '普通款'
	} else {
		return ''
	}
}


// 图片路径
export const getImgUrl = (url) => {
	return `${config.imgUrl}/${url}`
}
